
export OUT=$(cygpath 'C:\dev\java\harliane-gui\main\src\java\fr\harlie\harliane\model')

while [ $# -gt 0 ]
do
export CL=$(basename $1 .php)

echo "[$(date '+%Y/%m/%d %H:%M:%S')] Convert file $1"
echo "[$(date '+%Y/%m/%d %H:%M:%S')] Generating class $CL"
grep "@var" $1 | sed -e "s/[ \t][ \t]*/ /g" -e "s/^ \* //g" -e "s/[$]//g" | awk -vclass=$CL -vout=$OUT 'BEGIN{ 
	FS=" "; 
	OFS=" "; 
}{
	fields[$3] = $2
} END {
	print "package fr.harlie.harliane.model;"
	print ""
	print ""
	print "public class", class, " implements java.io.Serializable { "
	
	print ""
	print "\tprivate static final long serialVersionUID = 1L;"
	print ""
	for (field in fields) {
		field_name=to_java_name(field)
		type=to_java_type(fields[field])
		print "\tprivate", type, field_name "; // @var", fields[field], field
	}
	print ""
	print "\tprivate java.beans.PropertyChangeSupport propertyChangeSupport;"
	print ""
	print "\tpublic", class "() {";
	print "\t\tthis.propertyChangeSupport = new java.beans.PropertyChangeSupport(this);";
	print "\t}";
	
	for (field in fields) {
		field_name=to_java_name(field)
		getter=to_java_get(field_name, fields[field])
		setter=to_java_set(field_name, fields[field])
		type=to_java_type(fields[field])
		print "\tpublic", type, getter, "() {"
		print "\t\treturn this." field_name ";"
		print "\t}"
		print "\tpublic void", setter, "(" type, field_name ") {"
		print "\t\t" type, " old =", getter "();"
		print "\t\tthis." field_name, "=", field_name ";"
		print "\t\tthis.propertyChangeSupport.firePropertyChange(\"" field_name "\", old,", field_name ");"
		print "\t}"
		print ""
	}
	print "\tpublic String toString() {"
	print "\t\treturn \"" class "[\" +"
	nextt=""
	for (field in fields) {
		field_name=to_java_name(field)
		print "\t\t\t\"" nextt field_name "=\" + this." field_name " +"
		nextt=","
	}
	print "\t\t\t\"]\";"
	print "\t}"
	
	print "}"
}

function to_java_name(field_name) {
	gsub("_", " ", field_name)
	split(field_name, words);
	field_name=""
	for (word in words) {
		word=words[word]
		l=length(word);
		field_name=field_name toupper(substr(word, 1, 1)) substr(word, 2, l-1) 
	}
	l=length(field_name);
	field_name=tolower(substr(field_name, 1, 1)) substr(field_name, 2, l-1) 
	return field_name
}

function to_java_get(field_name, type) {
	l=length(field_name);
	if ($2 == "bool" || $2 == "boolean")
		return "is" toupper(substr(field_name, 1, 1)) substr(field_name, 2, l-1) 
	else
		return "get" toupper(substr(field_name, 1, 1)) substr(field_name, 2, l-1) 
}

function to_java_set(field_name, type) {
	return "set" toupper(substr(field_name, 1, 1)) substr(field_name, 2, l-1) 
}

function file_exists(filename) {
	rc=system("test -f " filename)
	return (rc==0)
}

function is_array_type(type) {
	return (match(type, "\\[\\]$"));
}

function to_java_type(type) {
	is_array=false
	
	if (match(type, "\\[\\]$")) {
		is_array = true;
		gsub("\\[\\]$", "", type)
		type = to_java_type(type)
		if (type == null)
			return null;
		else
			return "java.util.List<" type ">";
	} else {
		types["string"] = "java.lang.String";
		types["int"] = "java.lang.Integer";
		types["integer"] = "java.lang.Integer";
		types["bool"] = "java.lang.Boolean";
		types["boolean"] = "java.lang.Boolean";
		types["date"] = "java.util.Date";
		
		if (type in types) {
			return types[type];
		} else if (file_exists(type ".php")) {
			return type;
		} else {
			return null;
		}
	}
}
' > $OUT/$CL.java

[ ! -d "$OUT/converter" ] && mkdir $OUT/converter

echo "[$(date '+%Y/%m/%d %H:%M:%S')] Generating JSON converter class ${CL}JsonConverter"
grep "@var" $1 | sed -e "s/[ \t][ \t]*/ /g" -e "s/^ \* //g" -e "s/[$]//g" | awk -vclass=$CL -vout=$OUT 'BEGIN{ 
	FS=" "; 
	OFS=" "; 
}{
	fields[$3] = $2
} END {
	print "package fr.harlie.harliane.model.converter;"
	print ""
	print "import java.io.StringReader;"
	print ""
	print "import org.apache.log4j.Logger;"
	print "import org.json.*;"
	print ""
	print "import fr.harlie.harliane.model." class ";"

	print ""
	print ""
	print "public class", class "JsonConverter { "
	print ""
	print "private static Logger logger = Logger.getLogger(" class "JsonConverter.class);"
	print ""
	print "\tprivate", class "JsonConverter() {";
	print "\t}";
	
	print "\tpublic static", class, "fromJSonString(String jsonString) {"
	print "\t\tStringReader reader = null;"
	print "\t\ttry {"
	print "\t\t\treader = new StringReader(jsonString);"
	print "\t\t\tJSONTokener tokener = new JSONTokener(reader);"
	print "\t\t\tJSONObject json = new JSONObject(tokener);"
	print "\t\t\treturn fromJSon(json);"
	print "\t\t} catch (Exception ex) {"
	print "\t\t\tlogger.error(\"Cannot parse json string \" + jsonString, ex);"
	print "\t\t} finally {"
	print "\t\t\tif (reader != null)"
	print "\t\t\t\treader.close();"
	print "\t\t}"
	print "\t\treturn null;"
	print "\t}"
	print ""
	print ""
	
	print "\tpublic static java.util.List<" class "> fromJSonStringArray(String jsonString) {"
	print "\t\tStringReader reader = null;"
	print "\t\ttry {"
	print "\t\t\treader = new StringReader(jsonString);"
	print "\t\t\tJSONTokener tokener = new JSONTokener(reader);"
	print "\t\t\tJSONArray jsonArray = new JSONArray(tokener);"
	print "\t\t\treturn fromJSonArray(jsonArray);"
	print "\t\t} catch (Exception ex) {"
	print "\t\t\tlogger.error(\"Cannot parse json string \" + jsonString, ex);"
	print "\t\t} finally {"
	print "\t\t\tif (reader != null)"
	print "\t\t\t\treader.close();"
	print "\t\t}"
	print "\t\treturn null;"
	print "\t}"
	print ""
	print ""
	
	print "\tpublic static String toJSonString(" class " object) {"
	print "\t\ttry {"
	print "\t\t\treturn toJSon(object).toString();"
	print "\t\t} catch (Exception ex) {"
	print "\t\t\tlogger.error(\"Cannot serialize object (of type " class ")\" + object, ex);"
	print "\t\t}"
	print "\t\treturn null;"
	print "\t}"
	print ""
	print ""
	
	print "\tpublic static String toJSonString(java.util.List<" class "> objects) {"
	print "\t\ttry {"
	print "\t\t\treturn toJSon(objects).toString();"
	print "\t\t} catch (Exception ex) {"
	print "\t\t\tlogger.error(\"Cannot serialize objects (of type List<" class ">)\" + objects, ex);"
	print "\t\t}"
	print "\t\treturn null;"
	print "\t}"
	print ""
	print ""
	
	print "\tpublic static", class, "fromJSon(JSONObject json) throws JSONException {"
	print "\t\t" class, "res = new", class, "();"
	print ""
	for (field in fields) {
		field_name=to_java_name(field)
		setter=to_java_set(field_name, fields[field])
		json_func=from_json_func(fields[field])
		type=to_java_type(fields[field])
		
		if (json_func != null) {
			if (match(fields[field], "\\[\\]$")) {
				print "\t\t{" 
				print "\t\t\tJSONArray jsonArray = json.getJSONArray(\"" field "\");"
				print "\t\t\tjava.util.List<" type "> list = new java.util.ArrayList<" type ">(jsonArray.length());"
				print "\t\t\tfor (int i = 0 ; i < jsonArray.length() ; i++) {;"
				print "\t\t\t\tlist.add(jsonArray." json_func "(i));"
				print "\t\t\t}"
				print "\t\t\tres." setter "(list);"
				print "\t\t}"
			} else {
				print "\t\tres." setter "(json." json_func "(\"" field "\"));"
			}
		} else {
			if (match(fields[field], "\\[\\]$")) {
				print "\t\t{" 
				print "\t\t\tJSONArray jsonArray = json.getJSONArray(\"" field "\");"
				print "\t\t\tres." setter "(" type "JsonConverter.fromJSonArray(jsonArray));"
				print "\t\t}"
			} else {
				print "\t\tres." setter "(" type "JsonConverter.fromJSon(json.getJSONObject(\"" field "\")));"
			}
		}
	}
	print "\t\treturn res;"
	print "\t}"
	print ""
	print ""

	print "\tpublic static JSONObject toJSon(" class " object) throws JSONException {"
	print "\t\tJSONObject res = new JSONObject();"
	print ""
	for (field in fields) {
		field_name=to_java_name(field)
		getter=to_java_get(field_name, fields[field])
		json_func=from_json_func(fields[field])
		type=to_java_type(fields[field])
		
		if (json_func != null) {
			if (match(fields[field], "\\[\\]$")) {
				print "\t\t{" 
				print "\t\t\tJSONArray jsonArray = new JSONArray();"
				print "\t\t\tfor (int i = 0 ; i < jsonArray.length() ; i++) {;"
				print "\t\t\t\tjsonArray.put(i, object." getter "().get(i));"
				print "\t\t\t}"
				print "\t\t\tres.put(\"" field "\", jsonArray);"
				print "\t\t}"
			} else {
				print "\t\tres.put(\"" field "\", object." getter "());"
			}
		} else {
			print "\t\tres.put(\"" field "\", " type "JsonConverter.toJSon(object." getter "()));"
		}
	}
	print "\t\treturn res;"
	print "\t}"
	print ""
	print ""
	
	print "\tpublic static JSONArray toJSon(java.util.List<" class "> objects) throws JSONException {"
	print "\t\tJSONArray res = new JSONArray();"
	print ""
	print "\t\tfor (int i = 0; i < objects.size(); i++) {"
	print "\t\t\tres.put(i, toJSon(objects.get(i)));"
	print "\t\t}"
	print ""
	print "\t\treturn res;"
	print "\t}"
	print ""
	print ""
	
	print "\tpublic static java.util.List<" class "> fromJSonArray(JSONArray jsonArray) throws JSONException {"
	print "\t\tjava.util.List<" class "> list = new java.util.ArrayList<" class ">(jsonArray.length());"
	print "\t\tfor (int i = 0 ; i < jsonArray.length() ; i++) {;"
	print "\t\t\tlist.add(fromJSon(jsonArray.getJSONObject(i)));"
	print "\t\t}"
	print "\t\treturn list;"
	print "\t}"
	
	print "}"
}

function to_java_name(field_name) {
	gsub("_", " ", field_name)
	split(field_name, words);
	field_name=""
	for (word in words) {
		word=words[word]
		l=length(word);
		field_name=field_name toupper(substr(word, 1, 1)) substr(word, 2, l-1) 
	}
	l=length(field_name);
	field_name=tolower(substr(field_name, 1, 1)) substr(field_name, 2, l-1) 
	return field_name
}

function to_java_get(field_name, type) {
	l=length(field_name);
	if ($2 == "bool" || $2 == "boolean")
		return "is" toupper(substr(field_name, 1, 1)) substr(field_name, 2, l-1) 
	else
		return "get" toupper(substr(field_name, 1, 1)) substr(field_name, 2, l-1) 
}

function to_java_set(field_name, type) {
	return "set" toupper(substr(field_name, 1, 1)) substr(field_name, 2, l-1) 
}

function file_exists(filename) {
	rc=system("test -f " filename)
	return (rc==0)
}

function is_array_type(type) {
	if (match(type, "\\[\\]$")) {
		return 1;
	} else { 
		return 0;
	}
}

function to_java_type(type) {
	is_array=false
	
	if (match(type, "\\[\\]$")) {
		is_array = true;
		gsub("\\[\\]$", "", type)
		return to_java_type(type)
	} else {
		types["string"] = "java.lang.String";
		types["int"] = "java.lang.Integer";
		types["integer"] = "java.lang.Integer";
		types["bool"] = "java.lang.Boolean";
		types["boolean"] = "java.lang.Boolean";
		types["date"] = "java.util.Date";
		
		if (type in types) {
			return types[type];
		} else if (file_exists(type ".php")) {
			return type;
		} else {
			return null;
		}
	}
}

function from_json_func(type) {
	is_array=false
	
	if (match(type, "\\[\\]$")) {
		is_array = true;
		gsub("\\[\\]$", "", type)
	} 
	
	types["string"] = "optString";
	types["int"] = "optInt";
	types["integer"] = "optInt";
	types["bool"] = "optBoolean";
	types["boolean"] = "optBoolean";
	types["date"] = "optDate";
	
	if (type in types) {
		return types[type];
	} else {
		return null;
	}
}
' > $OUT/converter/${CL}JsonConverter.java
echo "[$(date '+%Y/%m/%d %H:%M:%S')] End"
echo ""
shift 
done



